const MongoClient = require('mongodb').MongoClient;
const ObjectId = require('mongodb').ObjectId;
// const url = 'mongodb://taosha_user:apyl8XWP6LSW4JHs@150.158.169.16:27777/taosha';
const url = 'mongodb://taosha_user:apyl8XWP6LSW4JHs@101.35.6.34:27722/taosha';

async function main() {
  for (let i = 83767; i < 300000; i = i + 10000) {
    console.log(i);
    const client = await MongoClient.connect(url, {useNewUrlParser: true, useUnifiedTopology: true});
    const db = client.db('taosha');

    let topics = await db.collection('topics')
      .find({})
      .skip(i)
      .limit(10000)
      .sort({_id: -1})
      .toArray();

    for (let i = 0; i < topics.length; i++) {
      let topic = topics[i];
      let result = await db.collection('topics').updateOne({_id: topic._id}, {
        $set: {
          author_id: topic.author_id.toString()
        }
      });
      console.log(i, topics.length);
    }

    client.close();
  }
}

main();
